<?php
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Http\Controllers\Api\v1\User\UserinfoController as Userinfo;

use Illuminate\Support\Facades\Input;
//
use DB;
class UserinfoController extends BaseController
{


    /*public function init()
    {
        $userinfo = $this->get_birth_data();

        $time = $this->deal_time(intval($userinfo['hour']),intval($userinfo['minute']),0,
            intval($userinfo['month']),intval($userinfo['day']),intval($userinfo['year']),
                                    $userinfo['is_summer'],intval($userinfo['timezone']));

        $setting = $this->get_astro_setting();

        $longitude = ($userinfo['long_deg'] + $userinfo['long_min'] / 60 ) * $userinfo['ew'];
        $latitude = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60 ) * $userinfo['ns'];

        return [$time[0],$time[1],$longitude,$latitude,$setting];
    }*/

    /**
     * 获取星盘出生数据
     */
   /* public function get_birth_data()
    {
        $is_public = intval(Input::get('public'));

        $id = intval(Input::get('id'));

        $userinfo = DB::table('recode')
            ->where(['id'=>$id])
            ->first([$id,'id','uid','name','type','year','month','day','hour','minute','timezone','long_deg','long_min','ew','lat_deg','lat_min','ns','is_summer']);

        if(empty($userinfo))
        {
            echo json_encode(['code'=>10711,'msg'=>'User info is not exist.']);
            exit;
        }
        if(!$is_public)
        {
             $uid = $request->uid;

            if($uid != $userinfo['uid'])
            {
                echo json_encode(['code'=>10712,'msg'=>'Can not get this userinfo.']);
            }
        }
        return $userinfo;
    }*/

    /**
     * 时间信息处理
     * $utdatenow
     * $utnow
     */
    public function deal_time($hour = null, $minute = null, $second = 0, $month = null,
                              $day = null, $year = null,$is_summer = 0,$timezone = 8)
    {
        // 年份限制 1900 - 2099
//        if($year === null || $year < 1800 || $year > 2199)
//        {
//            echo json_encode(['code'=>10713,'msg'=>'Year Invalid.']);
//            exit;
//        }
        // 月份限制 1-12
        if($month === null || $month < 0 || $month > 12)
        {
            echo json_encode(['code'=>10714,'msg'=>'Month Invalid.']);
            exit;
        }
        // 日期限制 1-31
        if($day === null || $day < 0 || $day > 31
            || ($this->is_bissextile($year) && $month == 2 && $day > 29)
            || (!$this->is_bissextile($year) && $month == 2 && $day > 28)
            || (in_array($month,[4,7,9,11]) && $day > 30))
        {
            echo json_encode(['code'=>10715,'msg'=>'Day Invalid.']);
            exit;
        }
        // 小时限制 0-23
        if($hour === null || $hour < 0 || $hour > 23)
        {
            echo json_encode(['code'=>10716,'msg'=>'Hour Invalid.']);
            exit;
        }
        // 分钟限制 0-59
        if($minute === null || $minute < 0 || $minute > 59)
        {
            echo json_encode(['code'=>10717,'msg'=>'Minute Invalid.']);
            exit;
        }
        if( $timezone < -12 || $timezone > 12)
        {
            echo json_encode(['code'=>10718,'msg'=>'Timezone Invalid.']);
            exit;
        }
        // 夏令时
        if($is_summer) $hour--;
        // 时区处理
        if ($timezone >= 0)
        {
            $whole = floor($timezone);
            $fraction = $timezone - floor($timezone);
        }
        else
        {
            $whole = ceil($timezone);
            $fraction = $timezone - ceil($timezone);
        }
        $hour = $hour - $whole;
        $minute= $minute- ($fraction * 60);

        $time = mktime($hour, $minute, $second, $month, $day, $year);

        return $time;

    }

    /**
     * 获取用户星盘配置
     */
    public function get_astro_setting()
    {
        $Set = new AstrosetController();

        $data = $Set->get();

        return $data;
    }

    /**
     * 判断是否为闰年
     * 闰年 true || 平年 false
     * @param null $year
     * @return bool
     */
    private function is_bissextile($year = null)
    {
        if($year === null)
        {
            echo json_encode(['code'=>10719,'msg'=>'Year Invalid.']);
            exit;
        }
        if($year % 400 === 0 || ($year % 100 !== 0 && $year % 4 === 0))
        {
            return true;
        }
        return false;
    }
}
